<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog v-model="dialogAuditUpdate.visible" :width="screenWidth" :fullscreen="mobile === 'mobile'"
    :title="dialogAuditUpdate.title" draggable close-icon="CloseBold" :close-delay="200" :loading="loading"
    @close="closeDialog(false)">
    <el-descriptions v-loading="loading" class="margin-top" :column="2" :size="size">
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="用地许可证编号">
        <Difference :old="detailInfo.oldInfo?.buildPlanNum" :now="detailInfo.newInfo?.buildPlanNum"></Difference>
      </el-descriptions-item>
      <!-- <el-descriptions-item
        label-class-name="cell-item"
        label-align="right"
        :label="detailInfo.oldInfo?.IndustryType == 1 ? '建筑市场监管平台项目编号' : '行业主管部门项目编号'"
      >
        <Difference :old="detailInfo.oldInfo?.industryPrjNum" :now="detailInfo.newInfo?.industryPrjNum"></Difference>
      </el-descriptions-item> -->
      <!-- <el-descriptions-item label-class-name="cell-item" label-align="right" label="所在城市">
        <Difference :old="fomartCityName(detailInfo.oldInfo?.areaCode)"
          :now="fomartCityName(detailInfo.newInfo?.areaCode)"></Difference>
      </el-descriptions-item> -->
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="施工许可证编号">
        <Difference :old="detailInfo.oldInfo?.builderLicences" :now="detailInfo.newInfo?.builderLicences"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="施工许可证发证日期">
        <Difference :old="detailInfo.oldInfo?.builderLicencesGrantDate"
          :now="detailInfo.newInfo?.builderLicencesGrantDate"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="施工许可证许可有效期">
        <Difference :old="detailInfo.oldInfo?.builderLicencesTermDate"
          :now="detailInfo.newInfo?.builderLicencesTermDate"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="中标合同价格">
        <Difference :old="detailInfo.oldInfo?.contractAmount" :now="detailInfo.newInfo?.contractAmount"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="合同开工日期">
        <Difference :old="detailInfo.oldInfo?.contractStartDate" :now="detailInfo.newInfo?.contractStartDate">
        </Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="计划竣工日期">
        <Difference :old="detailInfo.oldInfo?.contractEndDate" :now="detailInfo.newInfo?.contractEndDate"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="实际开工日期">
        <Difference :old="detailInfo.oldInfo?.startDate" :now="detailInfo.newInfo?.startDate"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="中标通知书编号">
        <Difference :old="detailInfo.oldInfo?.letterAcceptanceNum" :now="detailInfo.newInfo?.letterAcceptanceNum">
        </Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="招标标段编号">
        <Difference :old="detailInfo.oldInfo?.biddingSectionNum" :now="detailInfo.newInfo?.biddingSectionNum">
        </Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="多地施工点位">
        <Difference :old="detailInfo.oldInfo?.positionCount" :now="detailInfo.newInfo?.positionCount"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="联系人">
        <Difference :old="detailInfo.oldInfo?.linkMan" :now="detailInfo.newInfo?.linkMan"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="联系电话">
        <Difference :old="detailInfo.oldInfo?.linkPhone" :now="detailInfo.newInfo?.linkPhone"></Difference>
      </el-descriptions-item>
      <el-descriptions-item label-class-name="cell-item" label-align="right" label="上传中标通知书/施工合同" :span="2">
        <div style="display: inline-flex">
          <el-link v-for="item in detailInfo.oldAttachments" :key="item.attachmentId" type="success" title="点击下载"
            @click.stop="download(item)">{{ item.attachmentName }}</el-link>
        </div>
        <el-text v-if="detailInfo.newAttachments?.length > 0" type="danger">
          <Right style="width: 14px" />
        </el-text>
        <div style="display: inline-flex">
          <el-link v-for="item in detailInfo.newAttachments" :key="item.attachmentId" type="danger" title="点击下载"
            @click.stop="download(item)">{{ item.attachmentName }}</el-link>
        </div>
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handelAuditUpdate(true)">通过此次修改内容</el-button>
        <el-button :loading="loading" @click="handelAuditUpdate(false)">不予通过</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { UpdateApplyInfo, AuditUpdateProjectApply } from "@/api/project/projectInfo/index";
import { useScreen } from "@/store/modules/screen";
import { getDicts } from "@/store/modules/dict";
import Difference from "./components/Difference.vue";
import { downloadAttachment } from "@/api/common/other/index";
import { downloadFile } from "@/utils";

//声明变量
const areaTrees = getDicts("areaTrees")?.filter((item: any) => item.value === 540000);
const size = ref("");
const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenWidth = ref(900);
const itemSpan = ref(12);
const props = defineProps({
  dialogAuditUpdate: {
    type: Object,
    default: () => ({
      visible: false,
      title: "新增",
      footBtn: "确认新增",
      id: 0,
    }),
  },
});
const detailInfo = ref<any>({});
const { dialogAuditUpdate } = toRefs(props);
const emit = defineEmits(["sendClose"]);
const loading = ref(false);

//下载附件
const download = (item: any) => {
  downloadAttachment(item.attachmentId).then((res) => {
    downloadFile(res);
  });
};

const handelAuditUpdate = (type: boolean) => {
  loading.value = true;
  AuditUpdateProjectApply(props.dialogAuditUpdate.id, { auditType: type ? 1 : -1 })
    .then((res: any) => {
      if (res.success) {
        emit("sendClose", true);
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
    itemSpan.value = 24;
  } else {
    screenWidth.value = 900;
    itemSpan.value = 12;
  }
}
//初始化表单
const initForm = function () {
  if (dialogAuditUpdate.value.id) {
    loading.value = true;
    UpdateApplyInfo(props.dialogAuditUpdate.id)
      .then((res: any) => {
        if (res.success) {
          detailInfo.value = res.data;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }
};
const fomartCityName = function (cityCode: string) {
  let cityName = "";
  if (!cityCode) return "";
  areaTrees[0].children.forEach((i: any) => {
    if (i.value == cityCode) {
      console.log(i.label);
      cityName = i.label;
    }
    if (i.children) {
      i.children.forEach((j: any) => {
        if (j.value == cityCode) {
          console.log(j.label);
          cityName = j.label;
        }
      });
    }
  });
  return cityName;
};
watch(
  () => dialogAuditUpdate.value.visible,
  () => {
    detailInfo.value = {};
    if (dialogAuditUpdate.value.visible) {
      initForm();
    }
  }
);

onMounted(async () => {
  initScreen();
});

/**
 * 关闭弹窗
 */
function closeDialog(needRefresh: boolean) {
  emit("sendClose", needRefresh);
}
</script>

<style lang="scss" scoped></style>
